package com.example.cash.craft.dao.persist.repository.impl;

import com.example.cash.craft.dao.persist.mapper.CategoryMapper;
import com.example.cash.craft.dao.persist.repository.ICategoryRepository;
import com.example.cash.craft.pojo.entity.Category;
import com.example.cash.craft.pojo.vo.CategoryVO;
import com.example.cash.craft.pojo.vo.PageData;
import com.example.cash.craft.util.PageInfoToPageDataConverter;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CategoryRepositoryImpl implements ICategoryRepository {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public int insert(Category category) {
        return categoryMapper.insert(category);
    }

    @Override
    public int deleteById(Long id) {
        return categoryMapper.deleteById(id);
    }

    @Override
    public int updateById(Category category) {
        return categoryMapper.updateById(category);
    }

    @Override
    public int countByName(String name) {
        return categoryMapper.countByName(name);
    }

    @Override
    public CategoryVO selectById(Long id) {
        return categoryMapper.selectById(id);
    }

    @Override
    public PageData<CategoryVO> listAll(Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        List<CategoryVO> list = categoryMapper.listAll();
        PageInfo<CategoryVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }

}
